<?php

namespace App\Http\Middleware;

use Closure;
use Log;

class LogHttpRequest
{
    /**
     * @param         $request
     * @param Closure $next
     *
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        return $next($request);
    }

    /**
     * @param $request  \Illuminate\Http\Request
     * @param $response \Illuminate\Http\Response
     */
    public function terminate($request, $response)
    {
        $message = sprintf(
            '[%s] %s %s %s ',
            date('Y-m-d H:i:s'),
            $request->getClientIp(),
            $request->getRequestUri(),
            json_encode($request->all())
        );
        $message .= $response->getContent();                                 // append the its content
        Log::debug($message);  // also log the message
    }
}
